package com.yonyou.lxp.xutils3test.utils;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;

import java.util.ArrayList;

/**
 * Created by liuxiaopeng on 16/3/14.
 */
public class PermissionUtils {
    private final int SDK_PERMISSION_REQUEST = 127;
    private String permissionInfo;
    private Activity mContext;
    public  PermissionUtils (Activity mContext){
        this.mContext=mContext;
    }
    @TargetApi(23)
    public void getPersimmions(String permission) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ArrayList<String> permissions = new ArrayList<String>();
            /***
             * 定位权限为必须权限，用户如果禁止，则每次进入都会申请
             */
            // 定位精确位置
            if(mContext.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED){
                permissions.add(permission);
            }
            if (permissions.size() > 0) {
                mContext.requestPermissions(permissions.toArray(new String[permissions.size()]), SDK_PERMISSION_REQUEST);
            }
        }
    }

    @TargetApi(23)
    private boolean addPermission(ArrayList<String> permissionsList, String permission) {
        if (mContext.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { // 如果应用没有获得对应权限,则添加到列表中,准备批量申请
            if (mContext.shouldShowRequestPermissionRationale(permission)){
                return true;
            }else{
                permissionsList.add(permission);
                return false;
            }

        }else{
            return true;
        }
    }


    @TargetApi(23)
    public boolean checkSelfPermission(String permission){
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return true;
        }
        if(mContext.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED){
           return true;
        }
        return false;
    }
}
